package cn.bugDance.trigger.listener;

import cn.bugDance.domain.activity.model.entity.ActivityShopCartEntity;
import cn.bugDance.domain.activity.service.quota.IRaffleActivityQuotaService;
import cn.bugDance.domain.activity.service.IRaffleActivityService;
import cn.bugDance.domain.credit.model.entity.TradeEntity;
import cn.bugDance.domain.credit.model.valobj.TradeNameVO;
import cn.bugDance.domain.credit.model.valobj.TradeTypeVO;
import cn.bugDance.domain.credit.service.ICreditAccountAdjustService;
import cn.bugDance.domain.rebate.model.valobj.RebateDataVO;
import cn.bugDance.domain.rebate.model.valobj.RebateTypeVO;
import cn.bugDance.domain.rebate.service.IRebateQuotaService;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import cn.bugDance.types.response.BaseEvent;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
@RocketMQMessageListener(topic = "rebateTopic", consumerGroup = "rebate-consumer-group")
public class RebateListener implements RocketMQListener<String> {

    @Resource
    private IRaffleActivityQuotaService raffleActivityQuotaService;
    @Resource
    private IRaffleActivityService raffleActivityService;
    @Resource
    private IRebateQuotaService rebateQuotaService;
    @Resource
    private ICreditAccountAdjustService creditAccountAdjustService;

    @Override
    public void onMessage(String s) {
        try{
            BaseEvent<RebateDataVO> message = JSON.parseObject(s, new TypeReference<BaseEvent<RebateDataVO>>(){});
            log.info("【返利监听层-RebateListener】: 接收到返利结果");
            RebateDataVO data = message.getData();
            String rebateType = data.getRebateType();
            if(RebateTypeVO.sku.getCode().equals(rebateType)){
                Long sku = Long.valueOf(data.getRebateConfig());
                ActivityShopCartEntity activityShopCartEntity = new ActivityShopCartEntity();
                activityShopCartEntity.setSku(sku);
                activityShopCartEntity.setUserId(data.getUserId());
                activityShopCartEntity.setBizId(data.getBizId());
                raffleActivityService.createActivitySkuOrder(activityShopCartEntity);
            }
            //签到返利
            else if(RebateTypeVO.integral.getCode().equals(rebateType)){
                Integer credit = Integer.valueOf(data.getRebateConfig());

                TradeEntity tradeEntity = new TradeEntity();
                tradeEntity.setUserId(data.getUserId());
                tradeEntity.setBizId(data.getBizId());
                tradeEntity.setTradeAmount(credit);
                tradeEntity.setTradeName(TradeNameVO.REBATE);
                tradeEntity.setTradeType(TradeTypeVO.FORWARD);

                creditAccountAdjustService.createOrder(tradeEntity);
            }

        }catch (AppException e){
            if(ResponseCode.DUPLICATE_KEY_EXCEPTION.getCode().equals(e.getCode())){
                log.error("【返利监听层-RebateListener】: MQ消息重复");
            }
        }
        catch (Exception e){
            log.error("【返利监听层-RebateListener】: MQ消息接收失败");
            throw e;
        }
    }
}
